<template>
  <div :style="style" class="select-basic-data">
   <Select v-model="basicValue"
   :filterable="filterable"
   label-in-value
   @on-change="selectChange"
   @on-query-change="queryChange"
   :placeholder="placeholder"
   :placement="placement"
   :disabled="disabled"
   clearable
   >
   <Option
   v-for="item in basicData"
   :value="item.code"
   :key="item.code"
   :disabled = "disabledIds[item.code]">{{ item.name }}</Option>
 </Select>
</div>
</template>


<script>
  import Ajax from "../../js/Ajax/index.js"
  import axios from 'axios'

    Ajax.init({
        baseUrl: "",          // 是否设置 http(s)://***.com/, 即 http_origin，默认为空
        apiList: {},    // http请求地址的字典型
        // checkToken: false     // 是否在每个请求的头部设置token，默认为true
    });

  var getData = Ajax.methods;
  var  DICTIONARY_URL;

  if (window.F2ESDK_CONFIG && window.F2ESDK_CONFIG.apiServer && window.F2ESDK_CONFIG.apiServer.DICTIONARY_URL){
      DICTIONARY_URL = window.F2ESDK_CONFIG.apiServer.DICTIONARY_URL;
  }

  const THIS_URL =  DICTIONARY_URL ||  'http://192.168.10.241:7100/system/dictionarys/items'; 


  export default {
    props: {
      // 创建时的默认值
      defaultValue: {
        type: String
      },
      // 创建时的默认值
      disabledIds: {
        type: Object,
        default: function(){
          return {'123456':false}
        }
      },
      //传入key值
      basicKey: {
        type: String
      },
      // 用户自定义URL
      basicUrl: {
        type: String
      },
      // 是否支持搜索
      filterable: {
        type: Boolean,
        default: false
      },
      // 是否禁用
      disabled: {
        type: Boolean,
        default: false
      },
      // 未输入时显示的提示
      placeholder: {
        default: '请选择'
      },
      // 下拉框展开方向
      placement: {
        type: String,
        default: 'bottom'
      }
    },
    data: function(){
      return {
        style: "width: 200px;",
        basicData:[],
        basicValue: '',
      };
    },
    methods :{

      //选中框事件
      selectChange: function (val){
        var that = this;
        this.$emit('input', val.value);
        this.$emit("select-change",val);

      },
      // 搜索行变化事件
      queryChange: function (query){
        // 传递事件
        this.$emit("query-change",query);
      },
      // 加载成功回调函数
      onloadSuccess: function(data){
        this.$emit("onload-success",data);
      },
      // 加载失败，返回错误信息
      onError: function(v){
        this.$emit("on-error",v);
      },
      // 加载前回调函数
      beforeLoad: function(){
        this.$emit("before-load");
      },
      //获取数据
      getData: function() {
        var that = this;
        var url;
        if(!that.basicUrl){ //没有自定义url
          url = THIS_URL + "?dictCode="+that.basicKey+"&language=zh_CN"
          
        }else{  //自定义url
          url = that.basicUrl;
        }
        getData.getUrl(url).then(function(res){
          if(res.data.resultCode==0){
            for(var i = 0; i < res.data.data.length; i++){

              that.basicData.push({
                code:res.data.data[i].itemCode,
                name:res.data.data[i].itemValue
              });

            }
            that.onloadSuccess(res.data.data);
          } else {
            that.onError(res.data.resultMsg);
          }  
        });  
      }
    },

    mounted: function(){
      this.beforeLoad();
      this.basicValue=this.defaultValue;
      this.getData();
    },

    watch: {
      defaultValue: function (newValue, oldValue){
        this.basicValue = newValue;
      }
    }

  }
</script>

<style scoped lang="scss">
  .select-basic-data {
    display: inline-block;
  }

</style>


